package com.cdd.agent.api.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdd.common.domain.entity.*;
import com.cdd.agent.api.domain.dto.*;
import com.cdd.agent.api.domain.vo.*;
import com.cdd.agent.api.mapper.AppCashWithdrawalMapper;
import com.cdd.agent.api.mapper.AppGuildAnchorMapper;
import com.cdd.agent.api.mapper.AppGuildMapper;
import com.cdd.agent.api.mapper.AppMingxiChongzhiMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cdd
 * @date 2025-08-08 10:57
 */
@Service
public class GuildUserCountService {

    @Resource
    private AppUserService appUserService;

    @Resource
    private AppUserCollectionService collectionService;

    @Resource
    private AppSetupService appSetupService;

    @Resource
    private AppGuildMapper appGuildMapper;

    @Resource
    private AppGuildAnchorMapper guildAnchorMapper;

    @Resource
    private AppCashWithdrawalMapper cashWithdrawalMapper;

    @Resource
    private AppMingxiChongzhiMapper mingxiChongzhiMapper;

    private static final String TG_URL_PREFIX = "http://inv.mocue.net/downloadPageM.html?/";
    public String getTgUrl(Integer userId) {
        return TG_URL_PREFIX + appUserService.get(userId).getCode();
    }

    public AgentGuildBaseVo getGuildBaseInfo(Integer userId) {
        AgentGuildBaseVo vo = new AgentGuildBaseVo();
        AppUser user = appUserService.get(userId);
        vo.setNick(user.getNick());
        vo.setUsername(user.getNick());
        vo.setPhone(user.getLoginname());
        AppUserCollection collection = collectionService.findByUserId(userId);
        if (collection != null) {
            vo.setAliname(collection.getAliname());
            vo.setAliphonenum(collection.getAliphonenum());
        }
        AppSetup setup = appSetupService.get();
        vo.setZbRechargeInvite(setup.getJinDouExchangeForDiamonds().doubleValue());
        return vo;
    }

    /**
     * @desc 数据中心
     */
    public AgentCountVo getCount(Integer userId) {
        AppUser user = appUserService.get(userId);
        AppGuild guild = appGuildMapper.getByUserId(userId);
        Assert.notNull(guild, "用户不是公会会员");
        Integer guildId = guild.getId();
        GuildCountDto dto = new GuildCountDto(userId, guildId, user.getCode());
        AgentCountVo count = appUserService.getBaseMapper().getCount(dto);
        count.setYjyxUserNum(count.getYjyxWomanNum() + count.getYjyxManNum());
        return count;
    }

    public List<GuildUserOwnerCountVo> getGuildUserCount1(AgentCountDto dto) {
        return appUserService.getBaseMapper().getGuildUserCount1(dto);
    }

    public List<GuildDevoteCountVo> getGuildUserCount2(AgentCountDto dto) {
        return appUserService.getBaseMapper().getGuildUserCount2(dto);
    }

    /**
     * @desc 提现记录
     */
    public List<CashWithdrawalVo> getTxList(AgentCashWithdrawaltDto dto) {
        return cashWithdrawalMapper.getTxList(dto);
    }

    /**
     * @desc 工会主播下级充值明细
     */
    public List<MingxiChongzhiVo> getGuildUserCZList(MingxiChongzhiDto dto) {
        return mingxiChongzhiMapper.getGuildUserCZList(dto);
    }

    /**
     * @desc 收益汇总
     */
    public GuildIncomeCountVo getGuildIncomeCount(Integer userId) {
        AppGuild guild = appGuildMapper.getByUserId(userId);
        Assert.notNull(guild, "用户不是公会会员");
        GuildCountDto countDto = new GuildCountDto(userId, guild.getId());
        return guildAnchorMapper.getGuildIncomeCount(countDto);
    }

    /**
     * @desc 获取工会提成比例
     */
    public ProfitRatioVo getProfitRatio(Integer userId) {
        AppGuild guild = appGuildMapper.getByUserId(userId);
        Assert.notNull(guild, "工会信息不存在");
        ProfitRatioVo vo = new ProfitRatioVo();
        vo.setProfitRatio(guild.getProfit_ratio());
        AppSetup setup = appSetupService.get();
        vo.setMaxRatio(setup.getJinDouExchangeForDiamonds().doubleValue() * 100);
        return vo;
    }

    /**
     * @desc 更新比例
     */
    public void saveGuildRatio(GuildRatioSetupDto setupDto){
        AppGuild guild = appGuildMapper.getByUserId(setupDto.getUserId());
        Assert.notNull(guild, "工会信息不存在");
        AppSetup setup = appSetupService.get();
        Assert.notNull(setup, "系统设置信息不存在");
        double maxRatio = setup.getJinDouExchangeForDiamonds().doubleValue() * 100;
        if (setupDto.getProfitRatio() > maxRatio) {
            throw new RuntimeException("比例不能大于" + maxRatio);
        }
        guild.setProfit_ratio(setupDto.getProfitRatio());
        appGuildMapper.updateById(guild);
    }

    /**
     * @desc 校验主播是否属于当前用户工会
     */
    public boolean validNotGuildUser(Integer userId, Integer anchorId) {
        AppGuild guild = appGuildMapper.getByUserId(userId);
        if (guild == null) {
            return true;
        }
        return guildAnchorMapper.selectCount(
                new LambdaQueryWrapper<AppGuildAnchor>()
                        .eq(AppGuildAnchor::getGuild_id, guild.getId())
                        .eq(AppGuildAnchor::getAnchor_id, anchorId)
        ) == 0;
    }
}
